package com.ruoyi.admin.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ruoyi.admin.domain.CouponOrder;
import com.ruoyi.admin.mapper.CouponOrderMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.CouponMapper;
import com.ruoyi.admin.domain.Coupon;
import com.ruoyi.admin.service.ICouponService;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

/**
 * 优惠券管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-17
 */
@Service
public class CouponServiceImpl implements ICouponService {
    @Autowired
    private CouponMapper couponMapper;
    private final Map<Long, Object> lockMap = new ConcurrentHashMap<>();
    @Autowired
    private CouponOrderMapper couponOrderMapper;

    /**
     * 查询优惠券管理
     *
     * @param id 优惠券管理主键
     * @return 优惠券管理
     */
    @Override
    public Coupon selectCouponById(Long id) {
        return couponMapper.selectCouponById(id);
    }

    /**
     * 查询优惠券管理列表
     *
     * @param coupon 优惠券管理
     * @return 优惠券管理
     */
    @Override
    public List<Coupon> selectCouponList(Coupon coupon) {
        return couponMapper.selectCouponList(coupon);
    }

    /**
     * 新增优惠券管理
     *
     * @param coupon 优惠券管理
     * @return 结果
     */
    @Override
    public int insertCoupon(Coupon coupon) {
        coupon.setCreateTime(DateUtils.getNowDate());
        return couponMapper.insertCoupon(coupon);
    }

    /**
     * 修改优惠券管理
     *
     * @param coupon 优惠券管理
     * @return 结果
     */
    @Override
    public int updateCoupon(Coupon coupon) {
        coupon.setUpdateTime(DateUtils.getNowDate());
        return couponMapper.updateCoupon(coupon);
    }

    /**
     * 批量删除优惠券管理
     *
     * @param ids 需要删除的优惠券管理主键
     * @return 结果
     */
    @Override
    public int deleteCouponByIds(Long[] ids) {
        return couponMapper.deleteCouponByIds(ids);
    }

    /**
     * 删除优惠券管理信息
     *
     * @param id 优惠券管理主键
     * @return 结果
     */
    @Override
    public int deleteCouponById(Long id) {
        return couponMapper.deleteCouponById(id);
    }

    /**
     * 多人同时抢票
     */
    @Transactional
    public boolean grabCoupon(Long userId, Long couponId) {
        Object lock = lockMap.computeIfAbsent(couponId, k -> new Object());
        Coupon coupon = couponMapper.selectCouponById(couponId);
        if (coupon.getCount() <= 0) {
            return false;
        }
        synchronized (lock) {
            int updateCount = couponMapper.reduceCouponStock(couponId);
            if (updateCount <= 0) {
                return false;
            }
        }
        //插入订单记录
        CouponOrder couponOrder = new CouponOrder();
        couponOrder.setUserId(userId);
        couponOrder.setCouponId(couponId);
        couponOrder.setCreateTime(DateUtils.getNowDate());
        couponOrder.setStatus(0L);
        CouponOrder existingOrder = couponOrderMapper.selectByUserAndCoupon(userId, couponId);
        if (existingOrder != null) {
            // 用户已经抢过该优惠券，直接返回失败
            return false;
        }
        couponOrderMapper.insertCouponOrder(couponOrder);
        return true;


    }
}
